package com.gy.smartmanagement.home;
import android.os.Bundle;
import android.widget.FrameLayout;
import android.widget.RadioGroup;

import com.gy.smartmanagement.R;
import com.gy.smartmanagement.base.activity.BaseToolbarActivity;
import com.gy.smartmanagement.control.ControlFragment;
import com.gy.smartmanagement.process.ProcessFragment;
import com.gy.smartmanagement.utils.ActivityUtils;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import butterknife.BindView;

public class MainActivity extends BaseToolbarActivity implements RadioGroup.OnCheckedChangeListener {

  @BindView(R.id.container)
  FrameLayout cotainer;
  @BindView(R.id.rd_group)
  RadioGroup rd_group;

  private Fragment currentFragment;
  private ControlFragment controlFragment;
  private ProcessFragment processFragment;

  @Override
  protected int getLayoutResId() {
    return R.layout.activity_main;
  }

  @Override
  protected void initFragment(Bundle savedInstanceState) {
    processFragment=ProcessFragment.newInstance();
    controlFragment=ControlFragment.newInstance();
    ActivityUtils.addFragmentToActivity(fragmentManager,processFragment,R.id.container,ControlFragment.TAG);
    currentFragment=processFragment;
    rd_group.setOnCheckedChangeListener(this);
  }

  @Override
  public void onCheckedChanged(RadioGroup group, int checkedId) {
    switch (checkedId){
      case R.id.rd_menu_office:

        break;

      case R.id.rd_menu_process:
        switchContent(currentFragment,processFragment,ProcessFragment.TAG);
        break;

      case R.id.rd_menu_control:
        switchContent(currentFragment,controlFragment,ControlFragment.TAG);
        break;

      case R.id.rd_menu_analysis:

        break;

      case R.id.rd_menu_system:

        break;
    }
  }


  //切换fragment
  private void switchContent(Fragment from, Fragment to, String tag){
    if (currentFragment != to) {
      currentFragment = to;
      FragmentTransaction transaction = fragmentManager.beginTransaction();
      if (!to.isAdded()) {    // 先判断是否被add过
        transaction.hide(from).add(R.id.container, to, tag).commitAllowingStateLoss(); //
        // 隐藏当前的fragment，add下一个到Activity中
      } else {
        transaction.hide(from).show(to).commitAllowingStateLoss(); // 隐藏当前的fragment，显示下一个
      }
    }
  }

}
